<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="text" name="purchaseOrderDetailId" id="purchaseOrderDetailId" style="display: none"
           class="layui-input">
    <input type="text" name="purchaseOrderId" id="purchaseOrderId" style="display: none"
           class="layui-input">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label required">产品</label>
            <div class="layui-input-block">
                <select lay-filter="item" name="prodId" id="prodId" lay-verify="required" lay-search="">
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label required">供应商</label>
            <div class="layui-input-block">
                <select name="supId" id="supId" lay-verify="required" lay-search="">
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="number" name="quantity" id="quantity"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <input type="text" name="unit" id="unit"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
                <input type="number" name="unitPrice" id="unitPrice"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">不含税单价</label>
            <div class="layui-input-block">
                <input type="number" name="taxFreeUnitPrice" id="taxFreeUnitPrice"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否暂定价</label>
            <div class="layui-input-block">
                <select name="isTempPrice" id="isTempPrice">
                    <option value="NO">否</option>
                    <option value="YES">是</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">换算比例</label>
            <div class="layui-input-block">
                <input type="number" name="conversionRatio" id="conversionRatio"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">统计数量</label>
            <div class="layui-input-block">
                <input type="number" name="statQuantity" id="statQuantity"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">统计单位</label>
            <div class="layui-input-block">
                <input type="text" name="statUnit" id="statUnit"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">包装数量</label>
            <div class="layui-input-block">
                <input type="number" name="packageQuantity" id="packageQuantity"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">包装单位</label>
            <div class="layui-input-block">
                <input type="text" name="packageUnit" id="packageUnit"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">包装换算比例</label>
            <div class="layui-input-block">
                <input type="number" name="packageConversionRatio" id="packageConversionRatio"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">交货日期</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="deliveryDate"
                       id="deliveryDate">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">交期天数</label>
            <div class="layui-input-block">
                <input type="number" name="deliveryDays" id="deliveryDays"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">提货方式</label>
            <div class="layui-input-block">
                <input type="text" name="pickupMethod" id="pickupMethod"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">交货地点</label>
            <div class="layui-input-block">
                <input type="text" name="deliveryLocation" id="deliveryLocation"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">海关编码</label>
            <div class="layui-input-block">
                <input type="text" name="hsCode" id="hsCode"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">税率</label>
            <div class="layui-input-block">
                <input type="number" name="taxRate" id="taxRate"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">不含税金额</label>
            <div class="layui-input-block">
                <input type="number" name="taxFreeAmount" id="taxFreeAmount"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">增值税金额</label>
            <div class="layui-input-block">
                <input type="number" name="taxAmount" id="taxAmount"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">金额</label>
            <div class="layui-input-block">
                <input type="number" name="totalAmount" id="totalAmount"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">币种</label>
            <div class="layui-input-block">
                <input type="text" name="currency" id="currency"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">价格方式</label>
            <div class="layui-input-block">
                <input type="text" name="priceMethod" id="priceMethod"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">购销协议号</label>
            <div class="layui-input-block">
                <input type="text" name="salesAgreementNo" id="salesAgreementNo"
                       value="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" name="remark" id="remark"
                       value="" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">交期条款</label>
        <div class="layui-input-block">
            <textarea name="deliveryTerms" id="deliveryTerms" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    let purchaseOrderId = $.common.getUrlParam("purchaseOrderId");
    let prodId = $.common.getUrlParam("prodId");
    let supId = $.common.getUrlParam("supId");
    let user = $.common.getUserInfo();
    let isShow =  $.common.getUrlParam("isShow");
    if(isShow){
        $('.layui-btn').detach();
    }
    layui.use(['form', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;
        laydate.render({
            elem: '#deliveryDate'
            , type: 'date'
        });
        
        //产品
        let callback = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#prodId').append(new Option('请选择', '', true));// 下拉菜单里添加元素
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.itemNo, item.id);
                        if (prodId) {
                            if (item.id == prodId) {
                                option.selected = true;
                            }
                        }
                        $('#prodId').append(option);// 下拉菜单里添加元素
                    });
                } else {
                    $.common.showErrorMessage(result.message)
                    return false;
                }
            }
        };
        $.xAjax('/item/select', 'POST', JSON.stringify({}), callback);
        
        //供应商
        let callback2 = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#supId').append(new Option('请选择', '', true));// 下拉菜单里添加元素
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.supplierName, item.id);
                        if (supId) {
                            if (item.id == supId) {
                                option.selected = true;
                            }
                        }
                        $('#supId').append(option);// 下拉菜单里添加元素
                    });
                } else {
                    $.common.showErrorMessage(result.message)
                    return false;
                }
            }
        };
        $.xAjax('/supplier/select', 'POST', JSON.stringify({}), callback2);
        
        if (prodId) {
            $("#prodId").attr("disabled", true);
        }
        if (supId) {
            $("#supId").attr("disabled", true);
        }
        //初始化下拉框
        form.render("select");

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.purchaseOrderId = purchaseOrderId;
            
            // 处理税率值：如果输入的是百分比形式，转换为小数形式
            if (data.field.taxRate && data.field.taxRate !== '') {
                let taxRate = parseFloat(data.field.taxRate);
                if (taxRate > 1) {
                    // 如果税率大于1，说明是百分比形式，需要除以100
                    data.field.taxRate = (taxRate / 100).toFixed(4);
                } else {
                    // 如果税率小于等于1，说明已经是小数形式，直接使用
                    data.field.taxRate = taxRate.toFixed(4);
                }
            }
            
            let url = "/purchaseOrderDetail/saveOrUpdate"
            let callback = {
                onSuccess: function (result) {
                    if (result.code == '0') {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);
                    } else {
                        $.common.showErrorMessage(result.message)
                        return false;
                    }
                }
            };
            $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
            return false;
        });
    });
</script>
</body>
</html>
